﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

namespace HQWeb.SysManage.RoleManage
{
    public partial class RoleAddUserList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                string strId = Request["pid"];
                strId = strId == "all" ? "" : strId;
                BianDropList();    //绑定分店
                this.txtPIdHid.Text = strId;
                if (Convert.ToBoolean(Session["IsAll"]) == false)
                {
                    LabTile.Style["display"] = "none";
                    drop_Branch.Style["display"] = "none";
                }
                string strBID = Session["BRANCH_ID"].ToString();
                drop_Branch.SelectedValue = strBID;
                BindList();
            }
        }

        #region 绑定列表数据
        //分店查询
        private void BianDropList()
        {
            BLL.SysManage.T_PUB_BRANCH branchBll = new BLL.SysManage.T_PUB_BRANCH();
            List<Model.SysManage.T_PUB_BRANCH> branchModel = branchBll.GetModelList("");
            drop_Branch.DataSource = branchModel;
            drop_Branch.DataTextField = "BRANCH_NAME"; //dropdownlist的Text的字段 
            drop_Branch.DataValueField = "BRANCH_ID";//dropdownlist的Value的字段 
            drop_Branch.DataBind();
            ListItem item = new ListItem("全部", "");
            item.Selected = true;
            drop_Branch.Items.Add(item);

        }
        //数据绑定
        private void BindList()
        {
            BLL.SysManage.T_PUB_USER bll = new BLL.SysManage.T_PUB_USER();
            string strNmae = this.txtName.Text.Trim();
            string strBranchID = drop_Branch.SelectedValue;
            GridView1.DataSource = bll.GetUserInfo(strBranchID, this.txtPIdHid.Text.Trim(), txtName.Text.Trim());
            GridView1.DataBind();
        }
        #endregion
        //查询
        protected void tbnSearch_Click(object sender, EventArgs e)
        {
            BindList();
        }

        #region 行绑定触发事件
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[1].Visible = false; //隐藏列,可以取得该隐藏列的信息 
            }
        }
        #endregion

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //strWhere = strWhere.Replace("order by operatetime DESC", "");
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                { // when click the "GO" Button
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    //GridViewRow pagerRow = GridView1.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {  // when click the first, last, previous and next Button
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }

                // check to prevent form the NewPageIndex out of the range


                BindList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 保存按钮事件
        protected void Save_ServerClick(object sender, EventArgs e)
        {
            try
            {
                BLL.SysManage.T_PUB_USERROLE userRoleBll = new BLL.SysManage.T_PUB_USERROLE();
                Model.SysManage.T_PUB_USERROLE userRoleModel ;

                IList<Model.SysManage.T_PUB_USERROLE> ilModel = new List<Model.SysManage.T_PUB_USERROLE>();
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {
                    GridViewRow row = GridView1.Rows[i];
                    bool isChecked = ((CheckBox)row.FindControl("checkboxname")).Checked;
                    if (isChecked)
                    {
                        string strRoleID = Session["idSign"].ToString();
                        string strUserID = GridView1.Rows[i].Cells[1].Text.Trim();
                        IList il = userRoleBll.GetModleByRoleAndUser(strUserID, strRoleID);
                        int iCount = il.Count;
                        if (iCount == 0)
                        {
                            userRoleModel = new Model.SysManage.T_PUB_USERROLE();
                            userRoleModel.USER_ID = Convert.ToInt32(strUserID);
                            userRoleModel.ROLE_ID = Convert.ToInt32(strRoleID);
                            // userRoleBll.addSystemRole(userRoleModel);
                            ilModel.Add(userRoleModel);
                        }
                    }
                }
               // operRecode.setOperation("增加", "SYSTEM_USER_ROLE", "用户角色管理");
                userRoleBll.addSysRoleAll(ilModel);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "角色添加用户", DateTime.Now);
              //  operRecode.operateSucess();
                Universal.JS.RunJS("window.close();");
            }
            catch (Exception ex)
            {
                Universal.JS.Alert("加入人员出错,原因:" + ex.Message.ToString() + "。请与管理员联系");
                Universal.JS.RunJS("window.location.reload();window.close();");
                return;
               
            }
        }
        #endregion

        #region 关闭按钮事件
        protected void Close_ServerClick(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Write("<script language=javascript>self.location.reload();window.close();</script>");
        }
        #endregion
    }
}